﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using DotSpatial.Data;
using DotSpatial.Serialization;



using DotSpatial;



namespace FXnaGis
{
   
    /// <summary>
    /// 提供所有关于图层的操作服务
    /// </summary>
   public  class LayerService:IList<FLayer>
    {

       public LayerService(FMapFrame frame)
        {


            this._layers = new List<FLayer>();




            this._mapFrame = frame;
           






 
        }

     


       

        #region Events


       ///// <summary>
       ///// Occurs if the maps should zoom to this layer.
       ///// </summary>
       //public event EventHandler<EnvelopeArgs> ZoomToLayer;

       /// <summary>
       /// Occurs when one of the layers in this collection changes visibility.
       /// </summary>
       public event EventHandler LayerVisibleChanged;

       /// <summary>
       /// Occurs when a layer is added to this item.
       /// </summary>
       public event EventHandler<LayerEventArgs> LayerAdded;

       /// <summary>
       /// Occurs when a layer is removed from this item.
       /// </summary>
       public event EventHandler<LayerEventArgs> LayerRemoved;


  

        /// <summary>
        /// 当当前图层变化时，这里我们假定一次只能编辑一个图层
        /// </summary>
        public event EventHandler SelectedLayerChanged;



       /// <summary>
       /// 这里我们标记选择被触发事件
       /// </summary>
        public event EventHandler SelectionChanged;


        #endregion


        #region 属性 字段

        //FLayerCollection _layers;

        //public FLayerCollection Layers
        //{
        //    get { return _layers; }
        //    set { _layers = value; }
        //}


        //无法存储
        [NonSerialized]
        FMapFrame _mapFrame;


        /// <summary>
        /// 通用绘制管理系统
        /// </summary>
        public FMapFrame MapFrame
        {
            get { return _mapFrame; }
            set
            {

                _mapFrame = value;

                foreach (FLayer layer in this._layers)
                {
                    layer.MapFrame = this._mapFrame;

                }

            }
        }




       /// <summary>
       /// 内部保护的应用
       /// </summary>

        protected List<FLayer> _layers;


    




        bool _isSuspendEvent = false;

        public bool IsSuspendEvent
        {
            get { return _isSuspendEvent; }
            protected set { _isSuspendEvent = value; }
        }

       


        FLayer _selectedLayer;

        public FLayer SelectedLayer
        {
            get { return _selectedLayer; }
            set {


                if (this._selectedLayer!=value)
                {
                    this._selectedLayer = value;

                     if (SelectedLayerChanged != null)
                     {
                         SelectedLayerChanged(this, EventArgs.Empty);

                     }


                    
                }



             
               
            }
        }





    



        /// <summary>
        /// The envelope that contains all of the layers for this data frame.  Essentially this would be
        /// the extents to use if you want to zoom to the world view.
        /// 这里保存的是全局范围的
        /// 为zoom等提供必要的支持
        /// </summary>
        public  Extent Extent
        {
            get
            {
                Extent ext = null;

                

                IList<FLayer> layers = this._layers;
                if (layers != null)
                {
                    foreach (FLayer layer in layers)
                    {
                        if (layer.Extent != null)
                        {
                            if (ext == null)
                            {
                                ext = (Extent)layer.Extent.Clone();
                            }
                            else
                            {
                                ext.ExpandToInclude(layer.Extent);
                            }
                        }
                    }
                }
                return ext;
            }
        }



       /// <summary>
       /// 当前以选图形下的选择区域
       /// </summary>
        public Extent SelectedExtent
        {
            get
            {
                Extent ext = null;



                IList<FLayer> layers = this._layers;
                if (layers != null)
                {
                    foreach (FLayer layer in layers)
                    {
                        if (layer.SelectedExtent != null)
                        {
                            if (ext == null)
                            {
                                ext = (Extent)layer.SelectedExtent.Clone();
                            }
                            else
                            {
                                ext.ExpandToInclude(layer.SelectedExtent);
                            }
                        }
                    }
                }
                return ext;
            }
        }

     



        #endregion






        #region 方法

        /// <summary>
        /// Resumes event sending and fires a ListChanged event if any changes have taken place.
        /// This will not track all the individual changes that may have fired in the meantime.
        /// </summary>
        public void ResumeEvents()
        {
            this._isSuspendEvent = false;
        }

        /// <summary>
        /// Temporarilly suspends notice events, allowing a large number of changes.
        /// </summary>
        public void SuspendEvents()
        {
            this._isSuspendEvent = true;

        }



       /// <summary>
       /// 读取所有的矢量图层
       /// </summary>
       /// <returns></returns>
        public List<FVectorLayer> GetVectorLayers()
        {
            List<FVectorLayer> layers = new List<FVectorLayer>();

            foreach (FLayer layer in this)
            {
                if (layer is FVectorLayer)
                {
                    layers.Add((FVectorLayer)layer);

                }
                else
                {
                    if (layer is FGroupLayer)
                    {
                       FGroupLayer  glayer = layer as FGroupLayer;


                       layers.AddRange(glayer.GetVectorLayers());

                        
                    }
 
                }
                
            }


            return layers;



 
        }


        /// <summary>
        /// 通过深度遍历，取得当前id的layers，之后再考虑别的
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public FLayer GetLayer(string id)
        {
            foreach (FLayer layer in this._layers)
            {
                if (layer.Id == id)
                {
                    return layer;

                }




                if (layer is FGroupLayer)
                {
                    if ((layer as FGroupLayer).GetLayer(id) != null)
                    {
                        return (layer as FGroupLayer).GetLayer(id);

                    }


                }



            }

            return null;

        }


       /// <summary>
       /// 返回所有的当前选择了的fshape
       /// </summary>
       /// <returns></returns>
        public List<FShape> GetSelectedShapes()
        {
            List<FShape> list = new List<FShape>();

            List<FVectorLayer> layers = this.GetVectorLayers();

            foreach (FVectorLayer layer in layers)
            {


                foreach (FShape shape in layer.SelectedShapes)
                {

                    list.Add(shape);

                }





            }


            return list;

        }



    



        protected void OnLayerAdded(FLayer layer)
        {



            layer.MapFrame = this.MapFrame;

            if (!this._isSuspendEvent)
            {

                //要求刷新视图

                this._mapFrame.RedrawBuffer();
                

                //目前竟然找不到触发此事件的理由了，是不是我2了
                //纠结中

                if (this.LayerAdded != null)
                {
                    this.LayerAdded(this, new LayerEventArgs(layer));

                }

            }
        }

        protected void OnLayerRemoved(FLayer layer)
        {

            layer.MapFrame = null;

            if (!this._isSuspendEvent)
            {

                //要求刷新视图

                this._mapFrame.RedrawBuffer();

            }
 
        }



        public void Select(Extent selectedArea, FSelectionMode smode, FSelectionOperationMode omode)
        {


            //先写默认的全局选择机制，总的来说还是比较方便的，但是很容易选错东西，造成多选

            foreach (FLayer layer in this)
            {
                layer.Select(selectedArea, smode, omode);

            }


            if (this.SelectionChanged!=null)
            {
                SelectionChanged(this, EventArgs.Empty);
                
            }



        }


        #endregion


        #region 实现的ILIST接口，也只能这样了

        public int IndexOf(FLayer item)
        {
            return this._layers.IndexOf(item);
        }

        public void Insert(int index, FLayer item)
        {
            this._layers.Insert(index, item);




        }

        public void RemoveAt(int index)
        {

            FLayer layer = this._layers[index];

            if (layer!=null)
            {
                this.Remove(layer);
                
            }

            





            
        }

        public FLayer this[int index]
        {
            get
            {
                return this._layers[index];
            }
            set
            {
                this._layers[index] = value;
            }
        }


        public void Add(FLayer item)
        {
            this._layers.Add(item);

            this.OnLayerAdded(item);

        }

        public void Clear()
        {
            this._layers.Clear();

            //图层清空后重绘
            this._mapFrame.RedrawBuffer();

        }

        public bool Contains(FLayer item)
        {
            return this._layers.Contains(item);
        }

        public void CopyTo(FLayer[] array, int arrayIndex)
        {
            this._layers.CopyTo(array, arrayIndex);

        }

        public int Count
        {
            get { return this._layers.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }


       /// <summary>
       /// 
       /// </summary>
       /// <param name="item"></param>
       /// <returns></returns>
        public bool Remove(FLayer item)
        {



            bool b= this._layers.Remove(item);

            this.OnLayerRemoved(item);

            return b;



        }

        public IEnumerator<FLayer> GetEnumerator()
        {
            return this._layers.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this._layers.GetEnumerator();
        }

        #endregion






    }
}
